package com.superlistaportable.productList;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import com.superlistaportable.resultList.ResultListActivity;
import com.superlistaportable.sales.SalesActivity;

public class ProductListController {

	private final ProductListView view;
	private final ProductListModel model;
	private final ProductListActivity activity;
	private String selectedSupermarket;
	private ProductListController controller = this;
	private boolean updating = false;
	
	public ProductListController(ProductListModel model, ProductListView view, ProductListActivity activity, Intent i) {
		this.view = view;
		this.model = model;
		this.activity = activity;
		Bundle extras = i.getExtras();
		if (extras != null) {
			selectedSupermarket = extras.getString("selectedSupermarket");
        }
	}
	
	public void setListeners() {
		
		view.getButtonNext().setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
		    	Intent intent = new Intent(activity, ResultListActivity.class);
		    	intent.putStringArrayListExtra("itemsWithStock",(ArrayList<String>) model.getItemsWithStock());
		    	intent.putExtra("totals", model.getTotals());
		    	activity.startActivity(intent);
			}
		});
		
		view.getButtonSales().setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(activity, SalesActivity.class);
				intent.putExtra("selectedSupermarket", selectedSupermarket);
				activity.startActivity(intent);
			}
		});
		
		view.getButtonReload().setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (preExecAsyncTask()) {
					Toast.makeText(activity.getApplicationContext(), "Actualizando...",Toast.LENGTH_SHORT).show();
					ProductListAsyncTask task = new ProductListAsyncTask();
			        task.execute(controller);
				}
			}
		});
	}
	synchronized public boolean preExecAsyncTask() {

		if (updating) return false;
		
		// Disable update
		disableUpdate();
		updating = true;
		return updating;
	}
	public synchronized void postExecAsyncTask(String result) {

		if (updating) {
			if(!result.equals("Error al acceder al server")){
				view.buildExpandableList();
				Toast.makeText(activity.getApplicationContext(), "Actualizacion Completa",Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(activity.getApplicationContext(), "Fallo la conexión",Toast.LENGTH_SHORT).show();
			}
			enableUpdate();
			updating = false;
		}
	}
	
	public void disableUpdate() {
		// Disable button
		getView().enableUpdatebutton(false);	
	}
	
	public void enableUpdate() {
		// Disable button
		getView().enableUpdatebutton(true);	
	}
	
	public ProductListModel getModel(){
		return model;
	}
	public ProductListView getView(){
		return view;
	}
	public String getSelectedSupermarket(){
		return selectedSupermarket;
	}
}
